﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Gp.Scripts.Core.Skill;
using Gp.Scripts.Data.Skills;
using Gp.Scripts.Equip;
using UnityEngine;

namespace Gp.Scripts.Core {
    /// <summary>
    ///  用于管理单位行动
    /// </summary>
    public class UnitSkillCom {
        public BaseUnit Unit { get; private set; }
        private List<SkillEntity> _abilities;
        public MoveSkillEntity MoveSkill { get; private set; }
        public WeaponAttackEntity WeaponAttack { get; private set; }
        public IEnumerable<SkillEntity> SkillEntities => _abilities;

        
        public IEnumerable<WeaponAbilityEntity> WeaponAbilities =>
            _abilities.Select(i => i as WeaponAbilityEntity).Where(i => i != null);

        public IEnumerable<MagicSkillEntity> MagicSkill =>
            _abilities.Select(i => i as MagicSkillEntity).Where(i => i != null);


        public IEnumerable<AbilitySkillEntity> AbilitySkill =>
            _abilities.Select(i => i as AbilitySkillEntity).Where(i => i != null);

        public IEnumerable<BaseReactionEntity> ReactionSkill =>
            _abilities.Select(i => i as BaseReactionEntity).Where(i => i != null);


        public IEnumerator ExecuteAttack(AttackContext args) {
            yield return WeaponAttack.ExecuteWeaponAttack(args);
        }

        public T GetSkillEntity<T>() where T : SkillEntity {
            return _abilities.FirstOrDefault(i => i is T) as T;
        }

        public T GetWeaponSkillEntity<T>(WeaponEntity weapon) where T : WeaponAbilityEntity {
            return _abilities.FirstOrDefault(i => i is T w && w.Weapon == weapon) as T;
        }


        public void Init(BaseUnit unit) {
            Unit = unit;
            // // 初始化技能实体容器
            _abilities = new List<SkillEntity>();
            
            var moveSkillData = new Skill.ConfigSkill(1, "移动", "移动技能", "", "", null, new List<SkillCost>()){};
            var atkSkillData = new Skill.ConfigSkill(2, "攻击", "攻击技能", "", "", null, new List<SkillCost>()){};
            
            // var weaponAttackSkillData = model.WeaponAttackEntityConfigEntityConfig;
            MoveSkill = SkillEntity.Create<MoveSkillEntity>(moveSkillData);
            WeaponAttack = SkillEntity.Create<WeaponAttackEntity>(atkSkillData);
            AddSkill(MoveSkill);
            AddSkill(WeaponAttack);
            //
            // // ====================== 初始化机制 ==================================
            // // 注册单位格挡反应
            // AddSkill(new BlockAttackReactionEntity(moveSkillData));
        }


        private readonly List<SkillEntity> _skillsCache = new();


        public void AddSkill(SkillEntity item) {
            if (item == null) return;
    
            if (!Unit.IsInit) {
                _skillsCache.Add(item);
            }
            else {
                AddSkillProcess(item);
            }
        }


        public void AfterInit() {
            // 初始化基础技能
            // MoveSkill.Init(Unit);
            // WeaponAttack.Init(Unit);
            
            foreach (var s in _skillsCache) {
                AddSkillProcess(s);
            }

            _skillsCache.Clear();
        }


        private void AddSkillProcess(SkillEntity item) {
            // 防止重复技能添加
            if (item is WeaponAbilityEntity we) {
                if (WeaponAbilities.Any(x => x.Weapon == we.Weapon && x.GetType() == we.GetType()))
                    return;
            }
            else if (_abilities.Any(x => x.GetType() == item.GetType())) {
                return;
            }


            item.Init(Unit);
            _abilities.Add(item);
        }

        
        
        public IEnumerator OnTurnStart() {
            return _abilities.Select(s => s.OnTurnStart()).GetEnumerator();
        }


        public IEnumerator OnTurnEnd() {
            return _abilities.Select(s => s.OnTurnEnd()).GetEnumerator();
        }

        public void OnDestroy()
        {
            _abilities = null;
        }
    }
}